package com.shiyang.seckill.controller;

import com.shiyang.seckill.domain.SeckillUser;
import com.shiyang.seckill.service.GoodsService;
import com.shiyang.seckill.service.SeckillUserService;
import com.shiyang.seckill.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author shiyang
 * @create 2020-07-19 1:00 下午
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    private static Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    private SeckillUserService seckillUserService;
    @Autowired
    private GoodsService goodsService;

//    @RequestMapping("/to_list")
//    public String toList(HttpServletResponse response, Model model,
//        @CookieValue(value=SeckillUserService.COOKIE_NAME_TOKEN, required = false) String cookieToken,
//        @RequestParam(value=SeckillUserService.COOKIE_NAME_TOKEN, required = false) String paramToken) {
//        // 有时候手机端不会把token放在cookie里面 而是通过参数传递上来 为了兼容 再从参数中取一次
//        if (StringUtils.isEmpty(cookieToken) && StringUtils.isEmpty(paramToken)) {
//            return "login";
//        }
//        String token = StringUtils.isEmpty(paramToken) ? cookieToken : paramToken;
//        // session是根据最后一次访问时间 + 过期时间来计算的有效期的
//        SeckillUser user = seckillUserService.getByToken(response, token);
//        model.addAttribute("user", user);
//        return "goods_list";
//    }

    // 通过拦截器简化cookie的验证操作
    @RequestMapping("/to_list")
    public String toList(Model model, SeckillUser user) {
        model.addAttribute("user", user);
        // 查询商品列表
        List<GoodsVo> goodsList = goodsService.listGoodsVo();
        model.addAttribute("goodsList", goodsList);
        return "goods_list";
    }

    @RequestMapping("/to_detail/{goodsId}")
    public String detail(Model model, SeckillUser user,
                         @PathVariable("goodsId")long goodsId) {
        model.addAttribute("user", user);

        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
        model.addAttribute("goods", goods);

        long startAt = goods.getStartDate().getTime();
        long endAt = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int seckillStatus = 0;
        int remainSeconds = 0;
        if(now < startAt ) {//秒杀还没开始，倒计时
            seckillStatus = 0;
            remainSeconds = (int)((startAt - now )/1000);
        }else  if(now > endAt){//秒杀已经结束
            seckillStatus = 2;
            remainSeconds = -1;
        }else {//秒杀进行中
            seckillStatus = 1;
            remainSeconds = 0;
        }
        model.addAttribute("seckillStatus", seckillStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        return "goods_detail";
    }

}
